/**
 * Created by yanlin on 2017/10/24.
 */
var role = $("input[name='role']").val();

var stnm_message = new Ext.data.JsonStore({
    url: '/qtIndex/stationIdAndNm/combo.action',
    fields: ['value', 'text']
});
stnm_message.load();
var stnm_message_combo_add = new Ext.form.ComboBox({
    name: 'stnm',
    fieldLabel: '测站名称',
    emptyText: '请选择',
    mode: 'local',
    editable: true,
    triggerAction: 'all',
    store: stnm_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'stcd',
    allowBlank: false,
    msgTarget: "side",
    queryParam: 'userinput',
    forceSelection: true,
    listeners: {
        beforequery: function (qe) {
            var para = qe.query;
        }
    }
});
var stnm_message_combo_upd = new Ext.form.ComboBox({
    name: 'stnm',
    fieldLabel: '测站名称',
    emptyText: '请选择',
    mode: 'local',
    editable: true,
    triggerAction: 'all',
    store: stnm_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'stcd',
    allowBlank: false,
    msgTarget: "side",
    queryParam: 'userinput',
    forceSelection: true,
    listeners: {
        beforequery: function (qe) {
            var para = qe.query;
        }
    }
});

var ssxmxx_message = new Ext.data.JsonStore({
    url: '/qtIndex/ssxmxx/combo.action',
    fields: ['value', 'text']
});
ssxmxx_message.load();
var ssxmxx_message_combo_add = new Ext.form.ComboBox({
    name: 'obitnm',
    fieldLabel: '施测项目',
    emptyText: '请选择',
    mode: 'local',
    editable: false,
    triggerAction: 'all',
    store: ssxmxx_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'obitcd',
    allowBlank: false,
    msgTarget: "side"
});
var ssxmxx_message_combo_update = new Ext.form.ComboBox({
    name: 'obitnm',
    fieldLabel: '施测项目',
    emptyText: '请选择',
    mode: 'local',
    editable: false,
    triggerAction: 'all',
    store: ssxmxx_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'obitcd',
    allowBlank: false,
    msgTarget: "side"
});

var mlx_message = new Ext.data.JsonStore({
    url: '/qtIndex/mlx/combo.action',
    fields: ['value', 'text']
});
mlx_message.load();
var mlx_message_combo_add = new Ext.form.ComboBox({
    name: 'tynm',
    fieldLabel: '码类型',
    emptyText: '请选择',
    mode: 'local',
    editable: false,
    triggerAction: 'all',
    store: mlx_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'tycd',
    allowBlank: false,
    msgTarget: "side"
});
var mlx_message_combo_udp = new Ext.form.ComboBox({
    name: 'tynm',
    fieldLabel: '码类型',
    emptyText: '请选择',
    mode: 'local',
    editable: false,
    triggerAction: 'all',
    store: mlx_message,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'tycd',
    allowBlank: false,
    msgTarget: "side"
});

var flag_store = new Ext.data.JsonStore({
    fields: ['value', 'text'],
    data: [
        {text: "是", value: 1},
        {text: "否", value: 0}
    ]
});

var flag_combo = new Ext.form.ComboBox({
    name: 'flag',
    fieldLabel: '首选码',
    emptyText: '请选择',
    mode: 'local',
    editable: false,
    triggerAction: 'all',
    store: flag_store,
    width: 150,
    valueField: 'value', // 值
    displayField: 'text',
    hiddenName: 'first_flag',
    allowBlank: false,
    msgTarget: "side"
});

var sskzmdygx_reader = new Ext.data.JsonReader({
    totalProperty: 'totalProperty',
    root: 'root'
}, [
    {
        name: 'stcd'
    },
    {
        name: 'tycd'
    },
    {
        name: 'obitcd'
    }, {
        name: 'sscd'
    }, {
        name: 'first_flag'
    },
    {
        name: 'frdt'
    }, {
        name: 'nt'
    }
]);

var sskzmdygx_addform = new Ext.form.FormPanel({
    labelAlign: 'right',
    frame: true,
    labelWidth: 150,
    url: '/qtIndex/sskzmdygx/add.action',
    bodyStyle: 'padding:30px',
    reader: sskzmdygx_reader,
    items: [
        stnm_message_combo_add,
        mlx_message_combo_add,
        ssxmxx_message_combo_add,
        {
            xtype: 'textfield',
            fieldLabel: '站码',
            width: 150,
            name: 'sscd'
        },
        {
            xtype: 'textfield',
            fieldLabel: '优先级',
            width: 150,
            maxLength: 1,
            name: 'first_flag'
        },
        //flag_combo,
        // {
        //     xtype: 'datefield',
        //     fieldLabel: '启用日期',
        //     width: 150,
        //     format: 'Y-m-d',
        //     name: 'frdt',
        //     emptyText: '请选择',
        //     allowBlank: true,
        //     msgTarget: "side"
        // },
        {
            xtype: 'datetimefield',
            fieldLabel: '启用日期',
            width: 150,
            format: 'Y-m-d H:i:s',
            name: 'frdt',
            //emptyText: '请选择',
            allowBlank: true,
            msgTarget: "side"
        },
        {
            xtype: 'textarea',
            fieldLabel: '备注',
            width: 150,
            name: 'nt'
        }
    ]
});
var sskzmdygx_updform = new Ext.form.FormPanel({
    labelAlign: 'right',
    frame: true,
    labelWidth: 150,
    url: '/qtIndex/sskzmdygx/update.action',
    bodyStyle: 'padding:30px',
    reader: sskzmdygx_reader,
    items: [
        stnm_message_combo_upd,
        mlx_message_combo_udp,
        ssxmxx_message_combo_update,
        {
            xtype: 'textfield',
            fieldLabel: '站码',
            width: 150,
            name: 'sscd'
        },
        {
            xtype: 'datefield',
            fieldLabel: '启用日期',
            width: 150,
            format: 'Y-m-d',
            name: 'frdt',
            emptyText: '请选择',
            allowBlank: true,
            msgTarget: "side"
        },
        {
            xtype: 'textarea',
            fieldLabel: '备注',
            width: 150,
            name: 'nt'
        }
    ]
});

$(document).ready(function () {
    if (role != 0 && role!= 1) {
        $("#add_button").attr('style', "display:none");
    }

    $.post("/qtIndex/xzqhName.action", function (data) {
        //console.log(data);
        $("#header").text(data + "实时库站码对应关系对照表");
        // if (data == "宁波市") {
        //     $("#add_button").attr('style', "display:none");
        // }

    });
    // var xzqh_message = new Ext.data.JsonStore({
    //     url: '/index/xzqhxx/combo.action',
    //     fields: ['value', 'text']
    // });
    // xzqh_message.load();
    // var xzqh_combo = new Ext.form.ComboBox({
    //     name: 'addvnm',
    //     fieldLabel: '上级行政区',
    //     emptyText: '请选择',
    //     mode: 'local',
    //     editable: true,
    //     triggerAction: 'all',
    //     store: xzqh_message,
    //     width: 150,
    //     valueField: 'value', // 值
    //     displayField: 'text',
    //     hiddenName: 'addvcd',
    //     allowBlank: true,
    //     renderTo: 'xzqh_select',
    //     msgTarget: "side",
    //     queryParam: 'userinput',
    //     forceSelection: true,
    //     listeners: {
    //         beforequery: function (qe) {
    //             var para = qe.query;
    //         },
    //         select: function (combo, record, index) {
    //             var select_szqh = record.get("value");
    //             $.post("/qtIndex/sskzmdygx/xzqhSelect.action?addvcd=" + select_szqh, function (data) {
    //                 t.ajax.reload();
    //             });
    //         }
    //     }
    // });
    // xzqh_combo.show();

    var t = $('#sskzm').DataTable({
        serverSide: true,
        ajax: {
            url: '/qtIndex/sskzmdygx/all.action',
            type: 'POST'
        },
        "scrollY": 500,
        "scrollX": true,
        "ordering": false,
        "columnDefs": [{
            "searchable": false,
            "orderable": false,
            "targets": 0
        },
        //     {
        //     'targets': 1,
        //     // 'checkboxes': {
        //     //     'selectRow': true
        //     // }
        //     'searchable': false,
        //     'orderable': false,
        //     'className': 'dt-body-center'
        // },
        {
            'targets': 9
        }
        ],
        "order": [[1, 'asc']],
        "columns": [
            {"defaultContent": ""},//第0列
            //第1列
            // {
            //     render: function (data, type, full, meta) {
            //         return '<input type="checkbox" name="id[]" value="' + full.stcd + '">';
            //     }
            // },
            //第2列
            {
                "data": "stnm", "defaultContent": ""
            },
            {
                "data": "stcd", "defaultContent": ""
            },
            {"data": "sscd", "defaultContent": ""},
            {"data": "obitnm", "defaultContent": ""},
            {"data": "tynm", "defaultContent": ""},
            {
                "data": "first_flag", "defaultContent": ""
                // render: function (a, b, c, d) {
                //     if (c.first_flag == 0) {
                //         return "";
                //     } else {
                //         return "<img src='/jsps/qt/images/true.png'>"
                //     }
                // }

            },
            {"data": "dtstr", "defaultContent": ""},
            {"data": "nt", "defaultContent": ""},
            {
                render: function (a, b, c, d) {
                    //c为当前行的对象
                    if(role == 0 || role == 1){
                        return "<a onclick= del('" + c.stcd + "','" + c.tycd + "','" + c.obitcd + "')><img src='/jsps/qt/images/delete.jpg' title='删除'></a>";
                    }else{
                        $("#action").css("display","none");
                        $("#header").attr('style','margin-bottom: 25px');
                        return "";
                    }
                }
            }
        ],
        "pagingType": "full_numbers",
        "pageLength": 30,
        "dom": '<"toolbar">rtip',
        "language": {
            "lengthMenu": "每页 _MENU_ 条记录",
            "zeroRecords": "没有找到记录",
            "info": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
            "infoEmpty": "无记录",
            "infoFiltered": "(从 _MAX_ 条记录过滤)",
            "sSearch": "查询:",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": " 上一页 ",
                "sNext": " 下一页 ",
                "sLast": " 尾页"
            }
        }
    });

    t.on('draw.dt', function () {
        // $('#sskzm-select-all').attr("checked", false);
        t.column(2).visible(false);
        t.column(0, {
            search: 'applied',
            order: 'applied'
        }).nodes().each(function (cell, i) {
            //i 从0开始，所以这里先加1
            i = i + 1;
            //服务器模式下获取分页信息，使用 DT 提供的 API 直接获取分页信息
            var page = t.page.info();
            //当前第几页，从0开始
            var pageno = page.page;
            //每页数据
            var length = page.length;
            //行号等于 页数*每页数据长度+行号
            var columnIndex = (i + pageno * length);
            cell.innerHTML = columnIndex;
        });
    });

    $('#search_button').on('click',function () {
        var srh = $("input[name='search_text']").val();
        t.search(srh).draw();
    });
    $("#search_input").bind('keyup',function (e) {
        if(e.keyCode == 13){//回车事件
            var srh = $("input[name='search_text']").val();
            t.search(srh).draw();
        }
    });

    // $('#sskzm-select-all').on('click', function () {
    //     // Get all rows with search applied
    //     var rows = t.rows({'search': 'applied'}).nodes();
    //     // Check/uncheck checkboxes for all rows in the table
    //     $('input[type="checkbox"]', rows).prop('checked', this.checked);
    // });

    $('#add_button').on('click', function (e) {
        Ext.onReady(function () {
            var add_form = sskzmdygx_addform;
            var add_win = new Ext.Window({
                layout: 'fit',
                title: '实时库站码对应关系添加',
                width: '500',
                height: '400',
                closeAction: 'hide',
                constrain: true,
                buttonAlign: 'center',
                resizable: false,
                modal: true,
                plain: true,
                items: [add_form],
                buttons: [
                    {
                        text: '增加',
                        handler: function () {
                            // alert(JSON.stringify(add_form.getForm().getValues()));
                            Ext.Ajax.request({
                                url: '/qtIndex/sskzmdygx/check.action',
                                params: {
                                    sscd: add_form.getForm().findField('sscd').getValue(),
                                },
                                success: function (response) {
                                    var respText = Ext.util.JSON.decode(response.responseText);
                                    if(!respText.success){
                                        Ext.MessageBox.confirm('删除', '存在相同的站码,确认添加？', function (btn) {
                                            if (btn == 'yes') {
                                                add_form.getForm().submit({
                                                    success: function (form, action) {

                                                        Ext.Msg.alert('提示',
                                                            '提交成功！');
                                                        add_form.getForm().reset();
                                                        add_win.hide();
                                                        t.ajax.reload();
                                                    },
                                                    failure: function () {
                                                        Ext.Msg.alert('提示',
                                                            '提交失败，请检查输入数据项！');
                                                    }
                                                });
                                            }
                                        });
                                    }else {
                                        add_form.getForm().submit({
                                            success: function (form, action) {

                                                Ext.Msg.alert('提示',
                                                    '提交成功！');
                                                add_form.getForm().reset();
                                                add_win.hide();
                                                t.ajax.reload();
                                            },
                                            failure: function () {
                                                Ext.Msg.alert('提示',
                                                    '提交失败，请检查输入数据项！');
                                            }
                                        });
                                    }

                                },
                                failure: function (response) {
                                    Ext.Msg.alert('提示',
                                        '实时库站码检查失败！');
                                }

                            });


                        }
                    },
                    {
                        text: '取消',
                        handler: function () {
                            add_form.getForm().reset();
                            add_win.hide();
                        }
                    }
                ]
            });
            add_win.on('show', function () {
            });
            add_win.show();
        });
    });

});

function edit(stcd, tycd, obitcd) {
    Ext.onReady(function () {
        var upp_form = sskzmdygx_updform;
        var upp_win = new Ext.Window({
            layout: 'fit',
            title: '实时库站码对应关系修改',
            width: '500',
            height: '400',
            closeAction: 'hide',
            constrain: true,
            buttonAlign: 'center',
            resizable: false,
            modal: true,
            plain: true,
            items: [upp_form],
            buttons: [
                {
                    text: '修改',
                    handler: function () {
                        upp_form.getForm().submit({
                            success: function (form, action) {
                                Ext.Msg.alert('提示',
                                    '提交成功！', function () {
                                        history.go();
                                    });
                                upp_form.getForm().reset();
                                upp_win.hide();

                            },
                            failure: function () {
                                Ext.Msg.alert('提示',
                                    '提交失败，请检查输入数据项！');
                            }

                        });
                    }

                },
                {
                    text: '取消',
                    handler: function () {
                        upp_win.hide();
                    }
                }
            ]
        });

        upp_win.on('show', function () {
            var url = "/qtIndex/sskzmdygx/getById.action?stcd=" + stcd + "&tycd=" + tycd + "&obitcd=" + obitcd;
            Ext.Ajax.request({
                url: url,
                success: function (resp, opts) {
                    var respText = Ext.util.JSON.decode(resp.responseText);
                    upp_form.getForm().findField('stcd').setValue(respText.stcd);
                    if (respText.tycd != '') {
                        upp_form.getForm().findField('tycd').setValue(respText.tycd);
                    }
                    if (respText.obitcd != '') {
                        upp_form.getForm().findField('obitcd').setValue(respText.obitcd);
                    }
                    if (respText.sscd != '') {
                        upp_form.getForm().findField('sscd').setValue(respText.sscd);
                    }
                    if (respText.frdt != '') {
                        upp_form.getForm().findField('frdt').setValue(respText.frdt);
                    }
                    if (respText.nt != '') {
                        upp_form.getForm().findField('nt').setValue(respText.nt);
                    }
                }

            });
        });

        upp_win.show();

    });
}

function del(stcd, tycd, obitcd) {
    Ext.MessageBox.confirm('删除', '确认删除？', function (btn) {
        if (btn == 'yes') {
            Ext.Ajax.request({
                url: '/qtIndex/sskzmdygx/delete.action',
                params: {
                    stcd: stcd,
                    tycd: tycd,
                    obitcd:obitcd
                },
                success: function (response) {
                    Ext.Msg.alert('提示',
                        '删除成功！');
                    history.go();
                },
                failure: function (response) {
                    Ext.Msg.alert('提示',
                        '删除失败！');
                }
            });
        }
    });
}